package com.wanmait.demo.webcontroller.usercontroller.personalController;

import com.wanmait.demo.pojo.*;
import com.wanmait.demo.service.AddressService;
import com.wanmait.demo.service.CommunityService;
import com.wanmait.demo.service.DistrictService;
import com.wanmait.demo.service.TownService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user/personalCenter/address")
public class AddressController {


    @Resource
    private DistrictService districtService;

    
    @RequestMapping("/delete")
    @ResponseBody
    public void deleteAddressById(Address address ){

        address.setEnable(true);

        addressService.deleteOneAddress(address);
        addressService.clearCache();

    }
    @RequestMapping("/setDefault")
    @ResponseBody
    public void setDefaultById(Address address, Integer userinfoId){


            address.setDefaultAdress(true);

            addressService.updateDefaultAddress(address,userinfoId);
            addressService.clearCache();

    }
    @RequestMapping("/district")
    @ResponseBody
    public List<District> getDistrices(){

        List<District> districtList=districtService.zjjfindAll();
        return districtList;
    }

    @Resource
    private TownService townService;
    @RequestMapping("/town")
    @ResponseBody
    public List<Town> getTowns(Town town){

        List<Town> townList=townService.findAllByDistrictId(town.getId());

        return townList;
    }

    @Resource
    private CommunityService communityService;
    @RequestMapping("/community")
    @ResponseBody
    public List<Community> getcommunitys(Community community){

        List<Community> communityList=communityService.findAllCommunityByTownId(community.getId());

        return communityList;
    }

    @Resource
    private AddressService addressService;
    @RequestMapping("/addressAdd")
    public void ToaddressAdd(){


    }

    @RequestMapping("/addressGl")
    public List<Address> ToaddressEdit(HttpServletRequest request){
/////////////////////////////////////////////////////////////
        List<Address> addressList= null;
        try {
            HttpSession session=request.getSession();
            Userinfo userinfo=(Userinfo)session.getAttribute("loginUser");
            Integer userid=userinfo.getId();

            addressList = addressService.selectListByUserId(userid);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return addressList;

    }

    @RequestMapping("/addressAdd/add")

    public String addAddress(Address address, HttpServletRequest request){
        HttpSession session=request.getSession();
        Userinfo loginUser=(Userinfo)session.getAttribute("loginUser");


        Userinfo userinfo=new Userinfo();
        address.setUserinfo(userinfo);



        address.getUserinfo().setId(loginUser.getId());
        addressService.clearCache();
        Integer count=addressService.queryExistAddress(address.getUserinfo().getId());

        if (count==0)
        {
            address.setDefaultAdress(true);
        }
        else{
            address.setDefaultAdress(false);
        }

        addressService.insertOneAddress(address);
        return "redirect:/user/personalCenter/address/addressGl";

    }

    @RequestMapping("address")
    public String address(){
        return "user/ysk/address";
    } //尹山坤
}
